﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CDB.LinqModel.Data;
using CDB.LinqData;
using CDB.LinqModel;
using System.IO;
using System.Configuration;

public partial class back_builderWork : System.Web.UI.Page
{
    #region Variable Declaration
    IDataRepository _dataObject = new DataRepository();
    BuilderWork builderWork;
    List<BuilderWork> builderWorks;
    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (Session["admin"] != null)
            {
                if (Request.QueryString != null)
                {
                    if (Request.QueryString["Id"] != null)
                        bindBuilderWork(Request.QueryString["Id"].ToString());
                }
            }
            else
                Response.Redirect("login.aspx");
        }
    }
    private void bindBuilderWork(string builderId)
    {
        builderWork = new BuilderWork(_dataObject);
        builderWorks = new List<BuilderWork>();
        builderWork.BuilderId = Convert.ToInt32(builderId);
        builderWorks = builderWork.GetAllWorksByBuilderId();

        if (builderWorks != null)
        {
            grdBuilderWorks.DataSource = builderWorks;
            grdBuilderWorks.DataBind();
        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        builderWork = new BuilderWork(_dataObject);
        builderWork.BuilderId = Convert.ToInt32(Request.QueryString["Id"].ToString());
        builderWork.Name = txtProjectName.Text;
        builderWork.DescriptionLine = txtDescription.Text;
        builderWork.City = txtCity.Text;
        builderWork.State = txtState.Text;
        builderWork.Year = txtYear.Text;
        if (uplArchitectWorkImage.HasFile)
            builderWork.ProjectImage = saveImage(uplArchitectWorkImage, builderWork.BuilderId);
        builderWork.Create();

        bindBuilderWork(Request.QueryString["Id"].ToString());
        clearControls();
    }
    private void clearControls()
    {
        txtProjectName.Text = string.Empty;
        txtDescription.Text = string.Empty;
        txtCity.Text = string.Empty;
        txtState.Text = string.Empty;
        txtYear.Text = string.Empty;
    }
    private string saveImage(FileUpload fuImage, int builderId)
    {
        try
        {
            string filename = Path.GetFileName(fuImage.FileName);
            string[] fileArray = filename.Split('.');
            if (!Directory.Exists(Server.MapPath(ConfigurationManager.AppSettings["BuilderWorkImagePath"].ToString() + builderId)))
            {
                Directory.CreateDirectory(Server.MapPath(ConfigurationManager.AppSettings["BuilderWorkImagePath"].ToString() + builderId));
            }

            fuImage.SaveAs(Server.MapPath(ConfigurationManager.AppSettings["BuilderWorkImagePath"].ToString() + builderId + "/" + filename));
            return (filename);
        }
        catch (Exception ex)
        {
            return (ex.Message);
        }
    }
}